/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   				   *
 *   warren.kenny@gmail.com   						   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef GSSOCKET_H
#define GSSOCKET_H

#include <gsCore/gsCore.h>

#include <gsNet/gsAddress.h>

#ifndef WIN32

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#else

#include <WinSock2.h>

#endif

class gsSocket
{
public:
	
	///
	///	Constructor(s)
	///
	
	/**
	 *	Default constructor, creates a handle to a TCP socket 
	 */
       	gsSocket();
	
	/**
	 * 	Constructs a gsSocket object with the given socket handle
	 * @param handle A valid socket handle
	 */
	gsSocket( gsInt handle );
	
	/**
	 *        Attempts to establish a connection to the remote host
	 * @param remoteHost The gsAddress object representing the address of the remote host
	 * @return 	- True if the connection was established
	 *		- False if the connection attempt failed
	 */
	gsBool establishConnection( gsAddress remoteHost );
	
	/**
	 *        Attempts to receive data from the host which this gsSocket object is connected to 
	 * @param dataBuffer The data buffer into which the received data will be places
	 * @param bufferSize The size of this buffer in bytes
	 * @return The number of bytes received from the remote host
	 */
	const gsInt receiveData( void *dataBuffer, const gsUInt32 bufferSize );
	 
	/**
	 *	Attempts to send data to the remote host which this gsSocket object is connected to	
	 * @param dataBuffer 
	 * @param bufferSize 
	 * @return The number of bytes sent to the remote host
	 */
	const gsInt sendData( const void *dataBuffer, const gsUInt32 bufferSize );
	
	/**
	 * 	Attempts to break the connection between us and the socket's endpoint
	 */
	void breakConnection();
	
	/**
	 * 	Default destructor
	 */
	~gsSocket();

	
	

protected:
		
	friend class gsSocketManager;
	
	gsInt getSocketHandle() const;
	
private:
	
	gsInt m_socketHandle;

};

#endif
